//
//  HXHomeTableViewController.swift
//  DemoSinaWeibo
//
//  Created by Mon on 15/11/23.
//  Copyright © 2015年 Mon. All rights reserved.
//

import UIKit

import SVProgressHUD

class HXHomeTableViewController: HXBaseTableViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        /// 判断用户是否登录,解决在访客视图的时候也显示bar的图片
        if !userLogin {
            return
        }
        
//        edgesForExtendedLayout = UIRectEdge.None
        setupNavigationBarItems()
        
        /// 获取微博数据
        HXStatus.loadStatus { (statuses, error) -> () in
            if error != nil {
                print("error:\(error)")
                SVProgressHUD.showErrorWithStatus("网络不给力...")
                return
            }
            
            print("加载到了statuses:\(statuses)")
        }
        
    }
    
    // 在首页界面设置导航栏按钮;
    // 由于导航栏需要显示的是图片,而且是有状态变化的,UIButton可以达到这个效果
    private func setupNavigationBarItems() {
        /// 设置左边按钮
        navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch")
        
        /// 设置右边按钮
        navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop")
        
//        let leftButton = UIButton()
//        leftButton.setBackgroundImage(UIImage(named: "navigationbar_friendsearch"), forState: UIControlState.Normal)
//        leftButton.setBackgroundImage(UIImage(named: "navigationbar_friendsearch_highlighted"), forState: UIControlState.Highlighted)
//        /// 需要为按钮设置frame才能显示出来,使用控件自适应barItem大小
//        leftButton.sizeToFit()
//        navigationItem.leftBarButtonItem = UIBarButtonItem(customView: leftButton)
        
//        let rightButton = UIButton()
//        rightButton.setBackgroundImage(UIImage(named: "navigationbar_pop"), forState: UIControlState.Normal)
//        rightButton.setBackgroundImage(UIImage(named: "navigationbar_pop_highlighted"), forState: UIControlState.Highlighted)
//        /// 需要为按钮设置frame才能显示出来,使用控件自适应barItem大小
//        rightButton.sizeToFit()
//        navigationItem.rightBarButtonItem = UIBarButtonItem(customView: rightButton)
        
        /// MARK: - 设置中间按钮
//        let homeBarCenterButton = HXHomeTitleButton()
//        homeBarCenterButton.setTitle("首页", forState: UIControlState.Normal)
//        homeBarCenterButton.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
//        homeBarCenterButton.setImage(UIImage(named: "navigationbar_arrow_down"), forState: UIControlState.Normal)
//        homeBarCenterButton.sizeToFit()
        // 想要中间控件显示图文并排,Button有这个效果;调用BarItem的titleView这个属性来实现
        
        //由于按钮的标题不是设定死了,是当前登录用户的账号名称
        //所以需要获取当前用户名
        let titleName = HXUserAccount.loadUserAccount()?.screen_name ?? "浪小花"
        let homeBarCenterButton = HXHomeTitleButton(title: titleName)
        navigationItem.titleView = homeBarCenterButton
        homeBarCenterButton.addTarget(self, action: "homeBarCenterButtonClick:", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    // MARK: - 按钮的点击事件
    func homeBarCenterButtonClick(button: UIButton) {
//        print("------");
        /// 点击修改按钮的点击状态
        button.selected = !button.selected
        // 点击事件是在按钮被选中的情况下进行的
        // 故判断的是按钮的选择事件
/*---------------------------------------------------------------------------------------------*/
        /*
            UIView旋转动画特点:
                1.就近原则 --> 设270 路径:360-270=90
                2.如果一样远 --> 动画会顺时针旋转
         */
/*---------------------------------------------------------------------------------------------*/
//        if button.selected {
//            UIView.animateWithDuration(0.25, animations: { () -> Void in
//                button.imageView?.transform = CGAffineTransformMakeRotation(CGFloat(M_PI - 0.01))
//            })
//        }else {
//            UIView.animateWithDuration(0.25, animations: { () -> Void in
//                button.imageView?.transform = CGAffineTransformIdentity
//            })
//        }
        
        UIView.animateWithDuration(0.25) { () -> Void in
            button.imageView?.transform = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI - 0.01)) : CGAffineTransformIdentity
        }
    }
    
    
    
/*-------------------------------------------------------------------------------------------------*/
//    /// MARK: - 代码抽取
//    private func creatButton(imageName imageName: String) -> UIBarButtonItem {
//        let button = UIButton()
//        button.setBackgroundImage(UIImage(named: imageName), forState: UIControlState.Normal)
//        button.setBackgroundImage(UIImage(named: "\(imageName)_highlighted"), forState: UIControlState.Highlighted)
//        /// 需要为按钮设置frame才能显示出来,使用控件自适应barItem大小
//        button.sizeToFit()
//        
//        return UIBarButtonItem(customView: button)
//    }
}
